<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.3: pathdeform.cpp Example File (demos/deform/pathdeform.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">pathdeform.cpp Example File<br /><sup><sup>demos/deform/pathdeform.cpp</sup></sup></h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/</span>

 #include &quot;pathdeform.h&quot;

 #include &lt;QApplication&gt;
 #include &lt;QtDebug&gt;
 #include &lt;QMouseEvent&gt;
 #include &lt;QTimerEvent&gt;
 #include &lt;QLayout&gt;
 #include &lt;QLineEdit&gt;
 #include &lt;QPainter&gt;
 #include &lt;QSlider&gt;

 #include &lt;math.h&gt;

 PathDeformWidget::PathDeformWidget(QWidget *parent)
     : QWidget(parent)
 {
     setWindowTitle(&quot;Vector Deformation&quot;);

     m_renderer = new PathDeformRenderer(this);
     m_renderer-&gt;setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

     QGroupBox *mainGroup = new QGroupBox(this);
     mainGroup-&gt;setTitle(&quot;Vector Deformation&quot;);

     QGroupBox *radiusGroup = new QGroupBox(mainGroup);
     radiusGroup-&gt;setAttribute(Qt::WA_ContentsPropagated);
     radiusGroup-&gt;setTitle(&quot;Lens Radius&quot;);
     QSlider *radiusSlider = new QSlider(Qt::Horizontal, radiusGroup);
     radiusSlider-&gt;setRange(50, 150);
     radiusSlider-&gt;setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

     QGroupBox *deformGroup = new QGroupBox(mainGroup);
     deformGroup-&gt;setAttribute(Qt::WA_ContentsPropagated);
     deformGroup-&gt;setTitle(&quot;Deformation&quot;);
     QSlider *deformSlider = new QSlider(Qt::Horizontal, deformGroup);
     deformSlider-&gt;setRange(-100, 100);
     deformSlider-&gt;setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

     QGroupBox *fontSizeGroup = new QGroupBox(mainGroup);
     fontSizeGroup-&gt;setAttribute(Qt::WA_ContentsPropagated);
     fontSizeGroup-&gt;setTitle(&quot;Font Size&quot;);
     QSlider *fontSizeSlider = new QSlider(Qt::Horizontal, fontSizeGroup);
     fontSizeSlider-&gt;setRange(16, 200);
     fontSizeSlider-&gt;setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

     QGroupBox *textGroup = new QGroupBox(mainGroup);
     textGroup-&gt;setAttribute(Qt::WA_ContentsPropagated);
     textGroup-&gt;setTitle(&quot;Text&quot;);
     QLineEdit *textInput = new QLineEdit(textGroup);

     QPushButton *animateButton = new QPushButton(mainGroup);
     animateButton-&gt;setText(&quot;Animated&quot;);
     animateButton-&gt;setCheckable(true);

     QPushButton *showSourceButton = new QPushButton(mainGroup);
     showSourceButton-&gt;setText(&quot;Show Source&quot;);
<span class="comment"> //</span>     showSourceButton-&gt;setCheckable(true);
 #ifdef QT_OPENGL_SUPPORT
     QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
     enableOpenGLButton-&gt;setText(&quot;Use OpenGL&quot;);
     enableOpenGLButton-&gt;setCheckable(true);
     enableOpenGLButton-&gt;setChecked(m_renderer-&gt;usesOpenGL());
     if (!QGLFormat::hasOpenGL())
         enableOpenGLButton-&gt;hide();
 #endif
     QPushButton *whatsThisButton = new QPushButton(mainGroup);
     whatsThisButton-&gt;setText(&quot;What's This?&quot;);
     whatsThisButton-&gt;setCheckable(true);

     <span class="comment">//</span> Layouts
     QHBoxLayout *mainLayout = new QHBoxLayout(this);
     mainLayout-&gt;addWidget(m_renderer);
     mainLayout-&gt;addWidget(mainGroup);
     mainGroup-&gt;setFixedWidth(180);

     QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
     mainGroupLayout-&gt;addWidget(radiusGroup);
     mainGroupLayout-&gt;addWidget(deformGroup);
     mainGroupLayout-&gt;addWidget(fontSizeGroup);
     mainGroupLayout-&gt;addWidget(textGroup);
     mainGroupLayout-&gt;addWidget(animateButton);
     mainGroupLayout-&gt;addStretch(1);
     mainGroupLayout-&gt;addWidget(showSourceButton);
 #ifdef QT_OPENGL_SUPPORT
     mainGroupLayout-&gt;addWidget(enableOpenGLButton);
 #endif
     mainGroupLayout-&gt;addWidget(whatsThisButton);

     QVBoxLayout *radiusGroupLayout = new QVBoxLayout(radiusGroup);
     radiusGroupLayout-&gt;addWidget(radiusSlider);

     QVBoxLayout *deformGroupLayout = new QVBoxLayout(deformGroup);
     deformGroupLayout-&gt;addWidget(deformSlider);

     QVBoxLayout *fontSizeGroupLayout = new QVBoxLayout(fontSizeGroup);
     fontSizeGroupLayout-&gt;addWidget(fontSizeSlider);

     QVBoxLayout *textGroupLayout = new QVBoxLayout(textGroup);
     textGroupLayout-&gt;addWidget(textInput);

     connect(textInput, SIGNAL(textChanged(QString)), m_renderer, SLOT(setText(QString)));
     connect(radiusSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setRadius(int)));
     connect(deformSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setIntensity(int)));
     connect(fontSizeSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setFontSize(int)));
     connect(animateButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setAnimated(bool)));
     connect(whatsThisButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setDescriptionEnabled(bool)));
     connect(showSourceButton, SIGNAL(clicked()), m_renderer, SLOT(showSource()));
 #ifdef QT_OPENGL_SUPPORT
     connect(enableOpenGLButton, SIGNAL(clicked(bool)), m_renderer, SLOT(enableOpenGL(bool)));
 #endif
     connect(m_renderer, SIGNAL(descriptionEnabledChanged(bool)),
             whatsThisButton, SLOT(setChecked(bool)));

     animateButton-&gt;animateClick();
     deformSlider-&gt;setValue(80);
     radiusSlider-&gt;setValue(100);
     fontSizeSlider-&gt;setValue(120);
     textInput-&gt;setText(&quot;Qt&quot;);

     m_renderer-&gt;loadSourceFile(&quot;:res/pathdeform.cpp&quot;);
     m_renderer-&gt;loadDescription(&quot;:res/pathdeform.html&quot;);
     m_renderer-&gt;setDescriptionEnabled(false);
 }

 static inline QRect circle_bounds(const QPointF &amp;center, double radius, double compensation)
 {
     return QRect(qRound(center.x() - radius - compensation),
                  qRound(center.y() - radius - compensation),
                  qRound((radius + compensation) * 2),
                  qRound((radius + compensation) * 2));

 }

 const int LENS_EXTENT = 10;

 PathDeformRenderer::PathDeformRenderer(QWidget *widget)
     : ArthurFrame(widget)
 {
     m_radius = 100;
     m_pos = QPointF(m_radius, m_radius);
     m_direction = QPointF(1, 1);
     m_fontSize = 24;
     m_animated = true;
     m_repaintTimer.start(25, this);
     m_repaintTracker.start();
     m_intensity = 100;

<span class="comment"> //</span>     m_fpsTimer.start(1000, this);
<span class="comment"> //</span>     m_fpsCounter = 0;

     generateLensPixmap();
 }

 void PathDeformRenderer::setText(const QString &amp;text)
 {
     m_text = text;

     QFont f(&quot;times new roman,utopia&quot;);
     f.setStyleStrategy(QFont::ForceOutline);
     f.setPointSize(m_fontSize);
     f.setStyleHint(QFont::Times);

     QFontMetrics fm(f);

     m_paths.clear();
     m_pathBounds = QRect();

     QPointF advance(0, 0);

     bool do_quick = true;
     for (int i=0; i&lt;text.size(); ++i) {
         if (text.at(i).unicode() &gt;= 0x4ff &amp;&amp; text.at(i).unicode() &lt;= 0x1e00) {
             do_quick = false;
             break;
         }
     }

     if (do_quick) {
         for (int i=0; i&lt;text.size(); ++i) {
             QPainterPath path;
             path.addText(advance, f, text.mid(i, 1));
             m_pathBounds |= path.boundingRect();
             m_paths &lt;&lt; path;
             advance += QPointF(fm.width(text.mid(i, 1)), 0);
         }
     } else {
         QPainterPath path;
         path.addText(advance, f, text);
         m_pathBounds |= path.boundingRect();
         m_paths &lt;&lt; path;
     }

     for (int i=0; i&lt;m_paths.size(); ++i)
         m_paths[i] = m_paths[i] * QMatrix(1, 0, 0, 1, -m_pathBounds.x(), -m_pathBounds.y());

     update();
 }

 void PathDeformRenderer::generateLensPixmap()
 {
     double rad = m_radius + LENS_EXTENT;

     QRect bounds = circle_bounds(QPointF(), rad, 0);

     QPainter painter;

     if (preferImage()) {
         m_lens_image = QImage(bounds.size(), QImage::Format_ARGB32_Premultiplied);
         m_lens_image.fill(0);
         painter.begin(&amp;m_lens_image);
     } else {
         m_lens_pixmap = QPixmap(bounds.size());
         m_lens_pixmap.fill(QColor(0, 0, 0, 0));
         painter.begin(&amp;m_lens_pixmap);
     }

     QRadialGradient gr(rad, rad, rad, 3 * rad / 5, 3 * rad / 5);
     gr.setColorAt(0.0, QColor(255, 255, 255, 191));
     gr.setColorAt(0.2, QColor(255, 255, 127, 191));
     gr.setColorAt(0.9, QColor(150, 150, 200, 63));
     gr.setColorAt(0.95, QColor(0, 0, 0, 127));
     gr.setColorAt(1, QColor(0, 0, 0, 0));
     painter.setRenderHint(QPainter::Antialiasing);
     painter.setBrush(gr);
     painter.setPen(Qt::NoPen);
     painter.drawEllipse(0, 0, bounds.width(), bounds.height());
 }

 void PathDeformRenderer::setAnimated(bool animated)
 {
     m_animated = animated;

     if (m_animated) {
<span class="comment"> //</span>         m_fpsTimer.start(1000, this);
<span class="comment"> //</span>         m_fpsCounter = 0;
         m_repaintTimer.start(25, this);
         m_repaintTracker.start();
     } else {
<span class="comment"> //</span>         m_fpsTimer.stop();
         m_repaintTimer.stop();
     }
 }

 void PathDeformRenderer::timerEvent(QTimerEvent *e)
 {

     if (e-&gt;timerId() == m_repaintTimer.timerId()) {

         if (QLineF(QPointF(0,0), m_direction).length() &gt; 1)
             m_direction *= 0.995;
         double time = m_repaintTracker.restart();

         QRect rectBefore = circle_bounds(m_pos, m_radius, m_fontSize);

         double dx = m_direction.x();
         double dy = m_direction.y();
         if (time &gt; 0) {
             dx = dx * time * .1;
             dy = dy * time * .1;
         }

         m_pos += QPointF(dx, dy);

         if (m_pos.x() - m_radius &lt; 0) {
             m_direction.setX(-m_direction.x());
             m_pos.setX(m_radius);
         } else if (m_pos.x() + m_radius &gt; width()) {
             m_direction.setX(-m_direction.x());
             m_pos.setX(width() - m_radius);
         }

         if (m_pos.y() - m_radius &lt; 0) {
             m_direction.setY(-m_direction.y());
             m_pos.setY(m_radius);
         } else if (m_pos.y() + m_radius &gt; height()) {
             m_direction.setY(-m_direction.y());
             m_pos.setY(height() - m_radius);
         }

 #ifdef QT_OPENGL_SUPPORT
         if (usesOpenGL()) {
             update();
         } else
 #endif
         {
             QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
             update(rectAfter | rectBefore);
             QApplication::syncX();
         }
     }
<span class="comment"> //</span>     else if (e-&gt;timerId() == m_fpsTimer.timerId()) {
<span class="comment"> //</span>         printf(&quot;fps: %d\n&quot;, m_fpsCounter);
<span class="comment"> //</span>         emit frameRate(m_fpsCounter);
<span class="comment"> //</span>         m_fpsCounter = 0;

<span class="comment"> //</span>     }
 }

 void PathDeformRenderer::mousePressEvent(QMouseEvent *e)
 {
     setDescriptionEnabled(false);

     m_repaintTimer.stop();
     m_offset = QPointF();
     if (QLineF(m_pos, e-&gt;pos()).length() &lt;= m_radius)
         m_offset = m_pos - e-&gt;pos();

     mouseMoveEvent(e);
 }

 void PathDeformRenderer::mouseReleaseEvent(QMouseEvent *e)
 {
     if (e-&gt;buttons() == Qt::NoButton &amp;&amp; m_animated) {
         m_repaintTimer.start(10, this);
         m_repaintTracker.start();
     }
 }

 void PathDeformRenderer::mouseMoveEvent(QMouseEvent *e)
 {
     QRect rectBefore = circle_bounds(m_pos, m_radius, m_fontSize);
     if (e-&gt;type() == QEvent::MouseMove) {
         QLineF line(m_pos, e-&gt;pos() + m_offset);
         line.setLength(line.length() * .1);
         QPointF dir(line.dx(), line.dy());
         m_direction = (m_direction + dir) / 2;
     }
     m_pos = e-&gt;pos() + m_offset;
 #ifdef QT_OPENGL_SUPPORT
     if (usesOpenGL()) {
         update();
     } else
 #endif
     {
         QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
         update(rectBefore | rectAfter);
     }
 }

 QPainterPath PathDeformRenderer::lensDeform(const QPainterPath &amp;source, const QPointF &amp;offset)
 {
     QPainterPath path;
     path.addPath(source);

     double flip = m_intensity / 100.0;

     for (int i=0; i&lt;path.elementCount(); ++i) {
         const QPainterPath::Element &amp;e = path.elementAt(i);

         double x = e.x + offset.x();
         double y = e.y + offset.y();

         double dx = x - m_pos.x();
         double dy = y - m_pos.y();
         double len = m_radius - sqrt(dx * dx + dy * dy);

         if (len &gt; 0) {
             path.setElementPositionAt(i,
                                       x + flip * dx * len / m_radius,
                                       y + flip * dy * len / m_radius);
         } else {
             path.setElementPositionAt(i, x, y);
         }

     }

     return path;
 }

 void PathDeformRenderer::paint(QPainter *painter)
 {
     int pad_x = 5;
     int pad_y = 5;

     int skip_x = qRound(m_pathBounds.width() + pad_x + m_fontSize/2);
     int skip_y = qRound(m_pathBounds.height() + pad_y);

     painter-&gt;setPen(Qt::NoPen);
     painter-&gt;setBrush(Qt::black);

     QRectF clip(painter-&gt;clipPath().boundingRect());

     int overlap = pad_x / 2;

     for (int start_y=0; start_y &lt; height(); start_y += skip_y) {

         if (start_y &gt; clip.bottom())
             break;

         int start_x = -overlap;
         for (; start_x &lt; width(); start_x += skip_x) {

             if (start_y + skip_y &gt;= clip.top() &amp;&amp;
                 start_x + skip_x &gt;= clip.left() &amp;&amp;
                 start_x &lt;= clip.right()) {
                 for (int i=0; i&lt;m_paths.size(); ++i) {
                     QPainterPath path = lensDeform(m_paths[i], QPointF(start_x, start_y));
                     painter-&gt;drawPath(path);
                 }
             }
         }
         overlap = skip_x - (start_x - width());

     }

     if (preferImage()) {
         painter-&gt;drawImage(m_pos - QPointF(m_radius + LENS_EXTENT, m_radius + LENS_EXTENT),
                            m_lens_image);
     } else {
         painter-&gt;drawPixmap(m_pos - QPointF(m_radius + LENS_EXTENT, m_radius + LENS_EXTENT),
                             m_lens_pixmap);
     }
 }

 void PathDeformRenderer::setRadius(int radius)
 {
     double max = qMax(m_radius, (double)radius);
     m_radius = radius;
     generateLensPixmap();
     if (!m_animated || m_radius &lt; max) {
 #ifdef QT_OPENGL_SUPPORT
         if (usesOpenGL()) {
             update();
         } else
 #endif
         {
             update(circle_bounds(m_pos, max, m_fontSize));
         }
     }
 }

 void PathDeformRenderer::setIntensity(int intensity)
 {
     m_intensity = intensity;
     if (!m_animated) {
 #ifdef QT_OPENGL_SUPPORT
         if (usesOpenGL()) {
             update();
         } else
 #endif
         {
             update(circle_bounds(m_pos, m_radius, m_fontSize));
         }
     }
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
